package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.IWechatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.zzyl.framework.web.service.TokenService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService {
    @Autowired
    private FamilyMemberMapper familyMemberMapper;
    @Autowired
    private IWechatService wechatService;
    @Autowired
    private TokenService tokenService;

    static List<String> DEFAULT_NICKNAME_PREFIX = ListUtil.of("生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );


    /**
     * 微信登录
     *第一的登录 要获取appid 跟昵称
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public LoginVo login(UserLoginRequestDto dto) {
        //1.根据code调用微信api获取apenId
        String openId = wechatService.getOpenId(dto.getCode());

        //2.根据openId查询数据库获取用户信息family_member表看是否是新用户还是老用户
        //sql:select * from family_member where open_id=?
        FamilyMember familyMember = lambdaQuery().eq(FamilyMember::getOpenId, openId).one();

        //3.判断用户信息是否存在，如果不存在，说明是新用户(是老用户跳过这一步) 则准备新增，设置openid
        if (familyMember == null) {
            //说明是第一次登录 需要创建FamilyMember 去存储用户appid 跟昵称
            familyMember = new FamilyMember();
            familyMember.setOpenId(openId);
        }

        //4.调用微信api获取用户绑定手机号 同时获取手机号
        //如果是新用户则获取手机号 是老用户则更新手机号
        String phone = wechatService.getPhone(dto.getPhoneCode());
        familyMember.setPhone(phone);

        //5.新增或者修改family_member表数据
        if (familyMember.getId() == null) {
            String nickName = DEFAULT_NICKNAME_PREFIX.get((int) (Math.random() * DEFAULT_NICKNAME_PREFIX.size()))
                    + StringUtils.substring(familyMember.getPhone(), 7);
            familyMember.setName(nickName);
        }

        //这跟判断新增还是修改 ,根据id
        saveOrUpdate(familyMember);

        //将用户id 和昵称封装到jwt中
        Map<String, Object> claims = new HashMap<>();
        claims.put("nickName", familyMember.getName());
        claims.put("userId", familyMember.getId());
        String token = tokenService.createToken(claims);

       //6.将用户昵称以及token一并返回数据给前端
        LoginVo loginVo = LoginVo.builder().token(token).nickName(familyMember.getName()).build();
//        LoginVo loginVo = new LoginVo();
//        loginVo.setToken(token);
//        loginVo.setNickName(familyMember.getName());
        return loginVo;
    }
}